package ua.vn.ticket_scanner.network.task;

import android.os.AsyncTask;

import org.json.JSONException;
import org.json.JSONObject;

import ua.vn.ticket_scanner.network.data.ConnectResult;
import ua.vn.ticket_scanner.network.utils.ResultRunnable;

/**
 * Created by Ivan on 20.06.13.
 */
public abstract class SuperBaseTask extends AsyncTask<Void, Void, ConnectResult> {

    private static final String TAG = "SuperBaseTask";

    private final JSONObject json = new JSONObject();

    private Runnable preExecuter = null;
    private ResultRunnable postExecuter = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (preExecuter != null) {
            preExecuter.run();
        }
    }

    @Override
    protected void onPostExecute(ConnectResult connectResult) {
        super.onPostExecute(connectResult);
        if (postExecuter != null) {
            postExecuter.setConnectResult(connectResult);
            postExecuter.run();
        }
    }

    protected abstract void makeJSON();

    protected void putJSON(String key, String value) throws JSONException {
        json.put(key, value);
    }

    protected JSONObject getJSON() {
        return json;
    }

    public void setOnPreExecute(Runnable preExecuter) {
        this.preExecuter = preExecuter;
    }

    public void setOnPostExecute(ResultRunnable postExecuter) {
        this.postExecuter = postExecuter;
    }
}
